package me.zingon.doc.service.impl;

import me.zingon.doc.model.User;
import me.zingon.doc.service.UserProjectService;
import me.zingon.doc.dao.UserProjectDao;
import me.zingon.doc.model.UserProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
* Created by ZTCGenerator<zingon@aliyun.com> on 2018-5-31.
*/
@Service
public class UserProjectServiceImpl implements UserProjectService {

    @Autowired
    UserProjectDao userProjectDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insert(UserProject userProject){
        return userProjectDao.insert(userProject);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int delete(UserProject userProject){
        return userProjectDao.delete(userProject);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteByIds(Integer[] ids) {
        return  userProjectDao.deleteByIds(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int update(UserProject userProject){
        return userProjectDao.update(userProject);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public UserProject getById(Integer id){
        return userProjectDao.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<UserProject> list(UserProject userProject){
        return userProjectDao.list(userProject);
    }

    @Override
    public UserProject getByProjectIdWithUser(Integer projectId, User user) {
        return userProjectDao.list(new UserProject()
                .setProjectId(projectId)
                .setUserId(user.getId())
                .setDeleted(false)).get(0);
    }
}